package com.google.code.sms.controller;

import com.google.code.sms.command.MediaFolderSettingsCommand;
import com.google.code.sms.dao.MediaElementDao;
import com.google.code.sms.domain.MediaFolder;
import com.google.code.sms.service.MediaScannerService;
import com.google.code.sms.service.SettingsService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller for the page used to administrate the set of media folders.
 *
 * @author Sindre Mehus
 */
public class MediaFolderSettingsController extends SimpleFormController {

    private SettingsService settingsService;
    private MediaScannerService mediaScannerService;
    private MediaElementDao mediaElementDao;

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception 
    {
        MediaFolderSettingsCommand command = new MediaFolderSettingsCommand();

        if (request.getParameter("scanNow") != null) {
            mediaScannerService.scanLibrary();
        }
        
        if (request.getParameter("expunge") != null) {
            expunge();
        }
        
        if(request.getParameter("removeAll") != null)
        {
            mediaElementDao.deleteAllMediaElements();
        }

        command.setOrganizeByFolderStructure(settingsService.isOrganizeByFolderStructure());
        command.setScanning(mediaScannerService.isScanning());
        command.setMediaFolders(wrap(settingsService.getAllMediaFolders(true, true)));
        command.setNewMediaFolder(new MediaFolderSettingsCommand.MediaFolderInfo());
        command.setReload(request.getParameter("reload") != null || request.getParameter("scanNow") != null || request.getParameter("removeAll") != null);
        return command;
    }

    private void expunge() {
        mediaElementDao.expunge();
    }

    private List<MediaFolderSettingsCommand.MediaFolderInfo> wrap(List<MediaFolder> mediaFolders) {
        ArrayList<MediaFolderSettingsCommand.MediaFolderInfo> result = new ArrayList<>();
        for (MediaFolder mediaFolder : mediaFolders) {
            result.add(new MediaFolderSettingsCommand.MediaFolderInfo(mediaFolder));
        }
        return result;
    }

    @Override
    protected ModelAndView onSubmit(Object comm) throws Exception {
        MediaFolderSettingsCommand command = (MediaFolderSettingsCommand) comm;

        for (MediaFolderSettingsCommand.MediaFolderInfo mediaFolderInfo : command.getMediaFolders()) {
            if (mediaFolderInfo.isDelete()) {
                settingsService.deleteMediaFolder(mediaFolderInfo.getId());
            } else {
                settingsService.updateMediaFolder(mediaFolderInfo.toMediaFolder());
            }
        }

        MediaFolder newMediaFolder = command.getNewMediaFolder().toMediaFolder();
        if (newMediaFolder != null) {
            settingsService.createMediaFolder(newMediaFolder);
        }

        settingsService.setOrganizeByFolderStructure(command.isOrganizeByFolderStructure());
        settingsService.save();

        return new ModelAndView(new RedirectView(getSuccessView() + ".view?reload"));
    }

    public void setSettingsService(SettingsService settingsService) {
        this.settingsService = settingsService;
    }

    public void setMediaScannerService(MediaScannerService mediaScannerService) {
        this.mediaScannerService = mediaScannerService;
    }

    public void setMediaElementDao(MediaElementDao mediaElementDao) {
        this.mediaElementDao = mediaElementDao;
    }
}